package cn.com.infohold.smartcity.api;

import android.graphics.Color;
import android.net.Uri;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.File;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.bean.UserInfo;
import library.image.BitmapOption;
import library.image.ImageUtils;
import library.utils.DisplayUtil;

/**
 * Created by apple on 2017/4/21.
 */

public class  Dcms {

    public static void loadAvatar(ImageView view, Object src) {
        loadAvatar(view, src, false);
    }

    public static void loadAvatar(ImageView view, Object src, boolean round) {
        if (view == null)
            return;
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        int w = Math.max(0, lp.width);
        int h = Math.max(0, lp.height);
        loadAvatar(view, src, w, h, round);
    }

    public static void loadAvatar(ImageView view, Object src, int dimen) {
        loadAvatar(view, src, dimen, dimen, false);
    }

    public static void loadAvatar(ImageView view, Object src, int dimen, boolean round) {
        if (view == null || src == null)
            return;
        loadAvatar(view, src, dimen, dimen, round);
    }

    public static void loadAvatar(ImageView view, Object src, int w, int h) {
        loadAvatar(view, src, w, h, false);
    }

    public static void loadAvatar(ImageView view, Object src, int w, int h, boolean round) {
        if (view == null)
            return;

        BitmapOption option = null;
        if (src instanceof UserInfo) {
            UserInfo userInfo = (UserInfo) src;
            String url = userInfo != null ? userInfo.getAvatar() : null;

            if (url != null && url.startsWith("/")) {
                url = url.substring(1);
                option = SDK.api().getAvatar(url).image();
            } else {
                try {
                    option = ImageUtils.obtain(view.getContext())
                            .uri(Uri.parse(url));
                } catch (Exception e) {

                }
            }
        } else if (src instanceof File) {
            File file = (File) src;
            option = ImageUtils.obtain(view.getContext())
                    .file(file);
        }

        if (option == null) {
            option = ImageUtils.obtain(view.getContext())
                    .resource(R.mipmap.icon_default_avatar);

        }
        option.size(w, h)
                .round(round ? -1 : 0)
                .border(DisplayUtil.dp2px(1), Color.WHITE)
                .display(view);
    }


}
